/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.dialogboxes;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.bitBusy.manageyourriches.R;

/**
 * Class for dialg box with only two buttons
 * @author SumaHarsha
 *
 */
public class DialogWithThreeButtonsOnly implements DialogBox{

	
	/**Current context*/
	private Context myContext;
	
	/**Builder object*/
	private AlertDialog.Builder myBuilder;
	
	/** this object's View*/
	private View myView;
	
	/**Title*/
	private String myTitle;
	
	/**First button text*/
	private String myFirstButtonText;
	
	/**Second button text*/
	private String mySecondButtonText;
	
	/**Third button text*/
	private String myThirdButtonText;

	/**First button listener*/
	private OnClickListener myFirstButtonListener;

	/**Second button listener*/
	private OnClickListener mySecondButtonListener;

	/**Third button listener*/
	private OnClickListener myThirdButtonListener;

	/**Dialog*/
	private AlertDialog myDialog;


	/**Constructor*/
	public DialogWithThreeButtonsOnly(Context the_context)
	{
		if (the_context != null)
		{
			myContext = the_context;
			myBuilder =  new AlertDialog.Builder(myContext);
		}
	}
	
	/** Method to throw open the dialog box*/
	public void throwOpen()
	{
		if (myContext != null && myBuilder != null)
		{
			LayoutInflater inflater = LayoutInflater.from(myContext);
			View dialogView = inflater.inflate(R.layout.dialog_with_buttons_only, null);
			myView = dialogView; 
			myBuilder.setView(dialogView);
			showDialog();
		}
		else
		{
			throw new NullPointerException("Null pointer - cannot open dialog box!");
		}
	}

	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBox#showDialog()
	 */
	@Override
	public void showDialog() {
		if (myBuilder != null)
		{
			buildDialog();
			myDialog = myBuilder.show();
		}	
	}
	
	/**
	 * Method to set parameters
	 * @param the_title
	 * @param the_firstButtonText
	 * @param the_firstButtonListener
	 * @param the_secondButtonText
	 * @param the_secondButtonListener
	 */
	public void set(String the_title, String the_firstButtonText, OnClickListener the_firstButtonListener,
			String the_secondButtonText, OnClickListener the_secondButtonListener, String the_thirdButtonText,
			OnClickListener the_thirdButtonListener)
	{
			
			myTitle = the_title;
			myFirstButtonText = the_firstButtonText;
			myFirstButtonListener = the_firstButtonListener;
			mySecondButtonText = the_secondButtonText;
			mySecondButtonListener = the_secondButtonListener;
			myThirdButtonText = the_thirdButtonText;
			myThirdButtonListener = the_thirdButtonListener;
		
	}
	
	/** To build dialog
	 * 
	 */
	private void buildDialog() {
		setTitle();
		createDialog();

		Button first = (Button) myView.findViewById(R.id.dialogButtonOnly_firstButton);
		first.setText(myFirstButtonText);
		setButtonListener(first, myFirstButtonListener);
		
		Button second = (Button) myView.findViewById(R.id.dialogButtonOnly_secondButton);
		second.setText(mySecondButtonText);
		setButtonListener(second, mySecondButtonListener);

		
		Button third = (Button) myView.findViewById(R.id.dialogButtonOnly_thirdButton);
		third.setText(myThirdButtonText);
		setButtonListener(third, myThirdButtonListener);

}
	/**
	 * Sets title
	 */
	private void setTitle()
	{
		myBuilder.setTitle(myTitle);
	}
	
	/**
	 * Sets listener on button
	 * @param the_button
	 * @param the_ButtonListener
	 */
	private void setButtonListener(Button the_button, View.OnClickListener the_ButtonListener)
	{
		if (the_ButtonListener != null)
		{
			the_button.setOnClickListener(the_ButtonListener);
		}
		else
		{
			throw new NullPointerException("Listener is null");
		}
	}
	
	/**
	 * create 
	 */
	private void createDialog()
	{
		myBuilder.create();
	}
	

	/**
	 * cancel 
	 */
	public void cancel()
	{
		if (myDialog != null)
		{
			myDialog.dismiss();
		}
	}


}
